/**
 * 对话框防重复点击混入
 * 解决鼠标重复点击导致的弹窗闪烁问题
 */
export default {
  data() {
    return {
      globalDialogLock: false, // 全局弹窗锁
      lastClickTime: 0, // 上次点击时间
      clickThreshold: 300 // 点击间隔阈值(ms)
    }
  },
  methods: {
    /**
     * 检查是否为重复点击
     */
    isRepeatClick() {
      const now = Date.now();
      const timeDiff = now - this.lastClickTime;

      if (timeDiff < this.clickThreshold) {
        return true; // 重复点击
      }

      this.lastClickTime = now;
      return false;
    },

    /**
     * 关闭所有现有弹窗
     */
    closeAllDialogs() {
      // 关闭所有可能的弹窗状态
      const dialogStates = [
        'equipmentDialogVisible',
        'priorityDialogVisible',
        'reporterDialogVisible',
        'timeDialogVisible',
        'descriptionDialogVisible',
        'typeDialogVisible',
        'axisDialogVisible',
        'handlerDialogVisible',
        'solutionDialogVisible',
        'observationDialogVisible',
        'partDialogVisible',
        'acceptanceDialogVisible',
        'closureDialogVisible'
      ];

      dialogStates.forEach(state => {
        if (this[state] !== undefined) {
          this[state] = false;
        }
      });

      // 强制隐藏DOM中的弹窗
      const dialogs = document.querySelectorAll('.el-dialog__wrapper');
      dialogs.forEach(dialog => {
        dialog.style.display = 'none';
      });
    },

    /**
     * 创建智能遮盖层 - 只阻止页面卡片，不影响弹窗内组件
     */
    createBlockingOverlay() {
      // 移除现有遮盖层
      this.removeBlockingOverlay();

      // 创建新的遮盖层
      const overlay = document.createElement('div');
      overlay.id = 'dialog-blocking-overlay';
      overlay.style.cssText = `
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        z-index: 2500 !important;
        background: rgba(0,0,0,0.3) !important;
        pointer-events: auto !important;
        cursor: not-allowed !important;
      `;

      // 智能事件处理 - 只阻止点击页面卡片
      overlay.addEventListener('click', (e) => {
        // 检查点击目标是否是页面卡片
        const target = e.target;
        const isPageCard = target.closest('.info-card') ||
                          target.closest('.anomaly-card') ||
                          target.closest('.basic-info-card') ||
                          target.closest('.processing-info-card') ||
                          target.closest('.fault-analysis-card') ||
                          target.closest('.status-control-card');

        if (isPageCard) {
          e.stopPropagation();
          e.preventDefault();
          console.log('页面卡片点击被阻止');
          return false;
        }
      }, true);

      document.body.appendChild(overlay);

      // 同时添加CSS类
      const pageElement = document.querySelector('.workorder-page');
      if (pageElement) {
        pageElement.classList.add('click-locked');
      }
    },

    /**
     * 移除遮盖层
     */
    removeBlockingOverlay() {
      const overlay = document.getElementById('dialog-blocking-overlay');
      if (overlay) {
        overlay.remove();
      }

      const pageElement = document.querySelector('.workorder-page');
      if (pageElement) {
        pageElement.classList.remove('click-locked');
      }
    },

    /**
     * 安全打开弹窗 - 强力遮盖版本
     */
    safeOpenDialog(openFn) {
      // 检查重复点击
      if (this.isRepeatClick()) {
        console.log('重复点击被阻止');
        return;
      }

      // 检查全局锁
      if (this.globalDialogLock) {
        console.log('弹窗已锁定');
        return;
      }

      // 立即锁定并创建遮盖层
      this.globalDialogLock = true;
      this.createBlockingOverlay();

      // 先关闭所有现有弹窗
      this.closeAllDialogs();

      // 立即打开新弹窗
      try {
        openFn();

        // 确保弹窗在遮盖层之上
        this.$nextTick(() => {
          const dialogs = document.querySelectorAll('.el-dialog__wrapper');
          dialogs.forEach(dialog => {
            if (dialog.style.display !== 'none') {
              dialog.style.zIndex = '10000';
            }
          });
        });

      } catch (error) {
        console.error('弹窗打开错误:', error);
        this.removeBlockingOverlay();
        this.globalDialogLock = false;
      }

      // 延迟解锁
      setTimeout(() => {
        this.globalDialogLock = false;
      }, 500);
    },

    /**
     * 安全关闭弹窗
     */
    safeCloseDialog(closeFn) {
      try {
        closeFn();
      } catch (error) {
        console.error('弹窗关闭错误:', error);
      }

      // 立即移除遮盖层并解锁
      this.removeBlockingOverlay();
      this.globalDialogLock = false;
    }
  },

  // 组件销毁时清理
  beforeDestroy() {
    this.removeBlockingOverlay();
    this.globalDialogLock = false;
  }
}
